package com.net.shop.car.manager.ui.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.net.shop.car.manager.ui.view.GuaGuaLeGallery;

public class GuaGuaLeAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private Context mContext;
	private ArrayList<ImageView> list;
	private ImageView[] mImages;

	public GuaGuaLeAdapter(Context c,ArrayList<ImageView> list){
		mContext = c;
		this.list=list;
	}

	@SuppressWarnings("deprecation")
	public boolean createReflectedImages() {
		final int reflectionGap = 2;
		int index = 0;

		for (int i=0;i<list.size();i++) {
			Bitmap originalImage = ((BitmapDrawable)list.get(i).getDrawable()).getBitmap();
			int width = originalImage.getWidth();
			int height = originalImage.getHeight();

			Matrix matrix = new Matrix();
			matrix.preScale(1, -1);
			//0  起始X坐标   height 起始Y坐标  width截图的宽度   height/2截图的高度   
			Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
					height / 2, width, height /5, matrix, false);

			Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
					(height + height /5), Config.ARGB_8888);

			Canvas canvas = new Canvas(bitmapWithReflection);

			canvas.drawBitmap(originalImage, 0, 0, null);

			Paint deafaultPaint = new Paint();
			canvas.drawRect(0, height, width, height + reflectionGap,
					deafaultPaint);

			canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

			Paint paint = new Paint();
	//			LinearGradient shader = new LinearGradient(0, originalImage
	//					.getHeight(), 0, bitmapWithReflection.getHeight()
	//					+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
			LinearGradient shader=new LinearGradient(0,originalImage.getHeight(),0,bitmapWithReflection.getHeight()+reflectionGap, 0x70ffffff,0x00ffffff,TileMode.CLAMP);
			paint.setShader(shader);

			paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

			canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
					+ reflectionGap, paint);

		list.get(i).setImageBitmap(bitmapWithReflection);
		list.get(i).setLayoutParams(new GuaGuaLeGallery.LayoutParams(mContext.getResources().getDisplayMetrics().widthPixels/3,mContext.getResources().getDisplayMetrics().heightPixels/2));
//			imageView.setScaleType(ScaleType.MATRIX);
		}
		return true;
	}

	private Resources getResources() {
		// TODO Auto-generated method stub
		return null;
	}

	public int getCount() {
		return Integer.MAX_VALUE;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	
	public View getView(int position, View convertView, ViewGroup parent) {
	int i=position%list.size();
		return list.get(i);
		
	}

}
